//
//  YZPigDocCureViewModel.m
//  VET
//
//  Created by YZ on 2018/7/19.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigDocCureViewModel.h"
#import "YZPigDocCureModel.h"

@implementation YZPigDocCureViewModel

@synthesize dataSource = _dataSource;
@synthesize index = _index;

- (instancetype)init
{
    if(self = [super init]){
        [self config];
    }
    return self;
}

- (void)config
{
    _dataSource = @[[NSMutableArray array]];
}


- (void)headerRefreshSuccess:(void (^)(id))successBlock fail:(void (^)(NSError *))failBlock
{
    _index = 0;
    YZWeakSelf(self);
    NSMutableArray *arr = (NSMutableArray *)_dataSource.firstObject;
    [self reqestCureRecordListSuccess:^(NSDictionary *obj) {
        if (((NSArray *)((NSDictionary *)obj[@"data"])[@"list"]).count != 0) {
            weakself.totalPage = [((NSDictionary *)obj[@"data"])[@"lastPage"] integerValue] - 1;
        }else
        {
            weakself.totalPage = 0;
        }
        [arr removeAllObjects];
        for (NSDictionary *dic in (NSArray *)((NSDictionary *)obj[@"data"])[@"list"]) {
            YZPigDocCureModel *model = [YZPigDocCureModel mj_objectWithKeyValues:dic];
            [arr addObject:model];
        }
        if (successBlock) {
            successBlock(arr);
        }
    } fail:^(NSError *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)footerRefreshSuccess:(void (^)(id))successBlock fail:(void (^)(NSError *))failBlock
{
    _index++;
    NSMutableArray *arr = (NSMutableArray *)_dataSource.firstObject;
    [self reqestCureRecordListSuccess:^(NSDictionary *obj) {
        for (NSDictionary *dic in (NSArray *)((NSDictionary *)obj[@"data"])[@"list"]) {
            YZPigDocCureModel *model = [YZPigDocCureModel mj_objectWithKeyValues:dic];
            [arr addObject:model];
        }
        if (successBlock) {
            successBlock(((NSDictionary *)obj[@"data"])[@"list"]);
        }
    } fail:^(NSError *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)reqestCureRecordListSuccess:(YZNetworkCompletionSuccessBlock)successBlock fail:(YZNetworkCompletionFailBlock)failBlock;
{
    //objectType （猪只：swine、猪群：herd）
    self.loading = YES;
    NSDictionary *params = @{@"objectId":self.pigId,@"objectType":@"swine",@"index":[NSString stringWithFormat:@"%ld",_index],@"size":@"10"};
    [YZNetworkManager getWithApi:@"/api/treatment" params:params withHeaders:nil success:^(NSDictionary *obj) {
        self.loading = NO;
        if (successBlock) {
            successBlock(obj);
        }
    } fail:^(NSError *error) {
        self.loading = NO;
        if (failBlock) {
            failBlock(error);
        }
    }];
}
@end
